package com.yanggeng.demo.beginnerguidedemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

/**
 * Created by yanggeng on 2017/7/6.
 */

public class TestFragment extends Fragment implements GestureDetector.OnGestureListener {

    private SwipeMenuRecyclerView recyclerView;
    private String[] data = new String[50];
    private GestureDetector gestureDetector;
    private BottomSheetBehavior bottomSheetBehavior;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_common, null);
        ((TextView)view.findViewById(R.id.tv_title)).setText("首页");
        recyclerView = (SwipeMenuRecyclerView) view.findViewById(R.id.recycle_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//        gestureDetector = new GestureDetector(getActivity(), this);
//        bottomSheetBehavior = ((TabLayoutActivity)getActivity()).bottomSheetBehavior;
        for (int i = 0; i < 50; i ++) {
            data[i] = "第" + i + "条数据";
        }
//        view.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return gestureDetector.onTouchEvent(event);
//            }
//        });
//
//        recyclerView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return gestureDetector.onTouchEvent(event);
//            }
//        });

        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new SimpleViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.layout_simple_item, parent, false));
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ((SimpleViewHolder) holder).tvText.setText(data[position]);
//                ((SimpleViewHolder) holder).tvText.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        return gestureDetector.onTouchEvent(event);
//                    }
//                });
            }

            @Override
            public int getItemCount() {
                return data.length;
            }

            class SimpleViewHolder extends RecyclerView.ViewHolder {

                Button tvText;
                public SimpleViewHolder(View itemView) {
                    super(itemView);
                    tvText = (Button) itemView.findViewById(R.id.btn_province);
                }
            }
        });
        recyclerView.useDefaultLoadMore();

        recyclerView.addHeaderView(inflater.inflate(R.layout.layout_location_item, null));
        return view;
    }


    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float minMove = 50;
        float beginY = e1.getY();
        float endY = e2.getY();

//        if (endY - beginY > minMove && Math.abs(velocityY) > 0) { //下滑
//            if (bottomSheetBehavior.getState() == STATE_COLLAPSED) {
//                bottomSheetBehavior.setState(STATE_EXPANDED);
//                ToastUtil.centerToast(getActivity(), "下滑");
//            }
//        } else if (beginY - endY > minMove && Math.abs(velocityY) > 0) { //上滑
//            if (bottomSheetBehavior.getState() == STATE_EXPANDED) {
//                bottomSheetBehavior.setState(STATE_COLLAPSED);
//                ToastUtil.centerToast(getActivity(), "上滑");
//            }
//        }
        return false;
    }
}
